<?php

class Controller_Docs extends Controller_Abstract
{
    /**
     * 列出所有的文档
     */
    function actionIndex()
    {
        $this->_view['root_dir'] = rtrim(Q::ini('appini/docs/root_dir'), '/\\');
        $this->_view['books']    = Q::ini('appini/docs/books');
    }

    /**
     * 显示指定文档的指定页面
     */
    function actionPage()
    {
        $name = $this->_context->query('name');
        $file = $this->_context->query('file', 'index');

        $name = preg_replace('/[^a-z0-9\-]+/', '', $name);
        $file = preg_replace('/[^a-z0-9\-]+/', '', $file);
        $dir = rtrim(Q::ini('appini/docs/root_dir'), '/\\');
        $path = "{$dir}/{$name}/{$file}.php";

        if (empty($name) || empty($file) || !is_file($path))
        {
            return $this->_redirect(url('docs/index'));
        }

        $this->_view['doc_filename'] = $path;
    }
}

